<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="renderer" content="webkit">
<link rel="stylesheet" href="<%=path%>/css/pintuer.css">
<link rel="stylesheet" href="<%=path%>/css/admin.css">
<link rel="shortcut icon" href="<%=path%>/images/login.jfif">
<script src="<%=path%>/js/jquery.js"></script>

<link rel="stylesheet" type="text/css"
	href="<%=path%>/third/zTree_v3/css/zTreeStyle/zTreeStyle.css">
<script type="text/javascript"
	src="<%=path%>/third/zTree_v3/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript"
	src="<%=path%>/third/zTree_v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript"
	src="<%=path%>/third/zTree_v3/js/jquery.ztree.excheck.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
		//重新绑定表单提交
		$("#add_btn").bind("click", function() {
			$('form').submit();
		});

	});
</script>

</head>

<body>
	<div class="panel admin-panel">
		<div class="panel-head" id="add">
			<strong><span class="icon-pencil-square-o"></span>修改角色信息</strong>
		</div>
		<div class="body-content">
			<form id="form-addrole" method="post" class="form-x" action="">
				<input id="fid" name="fid" value="" type="hidden" />
				<div class="form-group">
					<div class="label">
						<label>角色编号：</label>
					</div>
					<div class="field field-icon-right">
						<input type="text" class="input w50 input input-big" value="${role.roleid}"
							name="roleid" id="roleid_add_input" placeholder="角色编号"
							data-validate="required:请输入角色编号" /> <span></span>
						<div class="tips"></div>
					</div>
				</div>
				<div class="form-group">
					<div class="label">
						<label>角色名称：</label>
					</div>
					<div class="field field-icon-right">
						<input type="text" class="input w50 input input-big" value="${role.rolename}"
							name="rolename"  id="rolename_add_input" data-validate="required:请输入角色编号" />
						<div class="tips"></div>
					</div>
				</div>
				<div class="form-group">
					<div class="label">
						<label>角色对应权限：</label>
					</div>
					<div class="field">

						<ul id="zTree" class="ztree" style="border: 1px solid #ddd;"></ul>
                        <span></span>
					</div>
				</div>

				<div class="form-group">
					<div class="label">
						<label></label>
					</div>
					<div class="field">
						<button id="submit_btn" class="button bg-main icon-check-square-o"
							type="button" >提交</button>
					</div>
				</div>
			</form>
		</div>
	</div>

</body>
<script type="text/javascript">
	//异步加载树形权限数据
	var setting = {
	
		check : {
			enable : true,
            chkboxType: { "Y": "", "N": "s" },
		},
		async : {
			type : "post",
			enable : true,
			url : "<%=path%>/role/menudata_loadbyroleid?roleid=${role.roleid}",
			autoParam : [ "id", "name=n", "level=lv" ]
		}
	};
	$(document).ready(function() {
		$.fn.zTree.init($("#zTree"), setting);
	});
	//ajax校验用户id是否重复
	$("#roleid_add_input").change(function() {
		var roleid = this.value;
		var treeObj = $.fn.zTree.getZTreeObj("zTree");
		var nodes = treeObj.getCheckedNodes(true);
		console.log(nodes);
		$.ajax({
			url : "<%=path%>/role/role_check",
			data : "roleid=" + roleid,
			type : "post",
			success : function(result) {
				if (result.code == 100) {
					show_validate_msg("#roleid_add_input", "success", "角色编号可用");
				} else {
					show_validate_msg("#roleid_add_input", "error", "角色编号不可用");
				}
			}
		})
	});
	function show_validate_msg(ele, status, msg) {
		//清除当前元素的校验状态
		$(ele).next("span").text("");
		if ("success" == status) {
			$(ele).next("span").text(msg);
		} else if ("error" == status) {
			$(ele).next("span").text(msg);
		}
	}
//修改角色权限
$("#submit_btn").click(function() {
        var roleid = $("#roleid_add_input").val();
	    var rolename = $("#rolename_add_input").val();
	    var treeObj = $.fn.zTree.getZTreeObj("zTree");
		var nodes = treeObj.getCheckedNodes(true);
		if(nodes.length == 0 ){
		alert("请选择权限!")
		}else{
		var menuids = [];
		$.each(nodes,function(i,node){
		   menuids.push(node.menuid);
		});
   $.ajax({
            type  :"POST",
		    url     :"<%=path%>/role/role_update_action",
		     data  :{
		            "roleid": roleid,
		            "rolename": rolename,
		            menuids: menuids,
		    },
		          traditional:true,
		          async: false,
		      success : function(result) {
				if (result.code == 100) {
					alert("修改成功！")
				} else {
					alert("修改失败！")
				}
			}
   })
   }
})
</script>
</html>

